package com.example.demo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.example.demo.adapter.ChatContactAdapter;
import com.example.demo.entity.ChatContact;

import java.util.ArrayList;
import java.util.List;

public class ContactActivity2 extends AppCompatActivity {

    public static final String CONTACT = "key_contact";

    private ChatContactAdapter adapter;
    private RecyclerView recyclerView;
    private List<ChatContact> contacts = new ArrayList<>();
    private Context context;
    private SwipeRefreshLayout refreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact2);
        context = ContactActivity2.this;

        recyclerView = findViewById(R.id.recycler_view);
        refreshLayout = findViewById(R.id.refreshLayout);

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
                if (refreshLayout.isRefreshing()){
                    refreshLayout.setRefreshing(false);
                }
            }
        });



        //1.设置布局管理器
        RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);

        //2.设置适配器
         adapter = new ChatContactAdapter(this);
        recyclerView.setAdapter(adapter);

        adapter.setChatOnClickListener(new ChatContactAdapter.ChatOnClickListener() {
            @Override
            public void onChatClick(ChatContact contact, int position) {
                fun();
//                contacts.get(position);
                Intent intent = new Intent(context, ChatActivity.class);
                startActivity(intent);
            }
        });

        //3.设置数据
        initData();
        adapter.setData(contacts);

        adapter.setChatOnClickListener(new ChatContactAdapter.ChatOnClickListener() {
            @Override
            public void onChatClick(ChatContact contact, int position) {
                Intent intent = new Intent(context, ChatActivity.class);
                intent.putExtra(ContactActivity2.CONTACT, contact);
                startActivity(intent);
            }
        });
    }

    private void initData(){
        for (int i = 0; i < 5; i++){
            ChatContact chatContact = new ChatContact();
            chatContact.setName("emo男孩" + i);
            if (i % 2 == 0){
                chatContact.setImg(R.drawable.a1);
            }
            else {
                chatContact.setImg(R.drawable.a2);
            }
            contacts.add(chatContact);
        }
        adapter.setData(contacts);
    }

   private void fun(){

   }
}